﻿'************************************************************************************'
'* Copyright (c) 2009, Patrick Driscoll All rights reserved.                        *'
'*                                                                                  *'
'* Redistribution and use in source and binary forms, with or without               *'
'* modification, are permitted provided that the following conditions are met:      *'
'*                                                                                  *'
'* Redistributions of source code must retain the above copyright notice, this      *'
'* list of conditions and the following disclaimer. Redistributions in binary       *'
'* form must reproduce the above copyright notice, this list of conditions and      *'
'* the following disclaimer in the documentation and/or other materials             *'
'* provided with the distribution. Neither the name of the DriscollArt Software nor *'
'* the names of its contributors may be used to endorse or promote products         *'
'* derived from this software without specific prior written permission.            *'
'*                                                                                  *'
'* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"      *'
'* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE        *'
'* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE       *'
'* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR      *'
'* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL           *'
'* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR       *'
'* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER       *'
'* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT               *'
'* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY        *'
'* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH      *'
'* DAMAGE.                                                                          *'
'************************************************************************************'

Imports System.Windows.Forms

''' <summary>
''' Table selection dialog
''' </summary>
''' <remarks></remarks>
Public Class Dialog_SelectTable

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Main.TextBox_SQLTableName.Text = ListBox_Tables.SelectedItem.ToString
        Main.DataGridViewFields.Rows.Clear()

        Dim objSQLInfo As New SQLTablesInfo(TextBox_ConnectionString.Text)
        Dim dtKeys As New System.Data.DataTable
        dtKeys = objSQLInfo.PrimaryKeys(ListBox_Tables.SelectedItem.ToString)
        For Each objRow As DataRow In objSQLInfo.Fields(ListBox_Tables.SelectedItem.ToString).Rows
            Main.DataGridViewFields.Rows.Add(IsPK(objRow.Item("column_name"), dtKeys), Not IsPK(objRow.Item("column_name"), dtKeys), IsPK(objRow.Item("column_name"), dtKeys), 1, IsPK(objRow.Item("column_name"), dtKeys), 0, objRow.Item("column_name"), objRow.Item("data_type"), GetMaxLength(objRow.Item("data_type"), objRow.Item("character_maximum_length")), GetControlType(objRow.Item("column_name"), objRow.Item("data_type")), objRow.Item("is_nullable") = "NO", StrConv(objRow.Item("column_name").ToString.Replace("_", " "), VbStrConv.ProperCase))
        Next

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Function GetMaxLength(ByVal strDataType As String, ByVal MaxLength As Object) As String
        Select Case strDataType
            Case "char", "varchar", "nvarchar"
                Return MaxLength
            Case Else
                Return ""
        End Select
    End Function
    Private Function GetControlType(ByVal strFieldName As String, ByVal strDataType As String) As String
        Select Case strFieldName.ToLower
            Case "userid", "user_id"
                Return "Audit:User"
            Case "date_modified", "datemodified"
                Return "Audit:Date"
            Case Else
                Select Case strDataType
                    Case "char", "varchar", "nvarchar"
                        Return "TextBox"
                    Case "text", "ntext"
                        Return "TextEditor"
                    Case "bit", "boolean"
                        Return "CheckBox"
                    Case Else
                        Return ""
                End Select
        End Select

    End Function

    Private Function IsPK(ByVal FieldName As String, ByVal TableRows As DataTable) As Boolean
        For Each objRow In TableRows.Rows
            If objRow.item("column_name") = FieldName Then
                Return True
                Exit For
            End If
        Next

        Return False
    End Function

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Connect.Click
        Label_PleaseWait.Visible = True
        Application.DoEvents()

        Dim objSQLInfo As New SQLTablesInfo(TextBox_ConnectionString.Text)
        ListBox_Tables.Items.Clear()

        For Each objrow As System.Data.DataRow In objSQLInfo.Tables.Rows
            ListBox_Tables.Items.Add(objrow.Item("table_name"))
        Next
        Label_PleaseWait.Visible = False
        OK_Button.Enabled = False
    End Sub

    Private Sub Dialog_SelectTable_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If CheckBox_Remember.Checked Then
            My.Settings.LastConnectionString = TextBox_ConnectionString.Text
        Else
            My.Settings.LastConnectionString = ""
        End If
    End Sub

    Private Sub Dialog_SelectTable_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If My.Settings.LastConnectionString <> "" Then
            TextBox_ConnectionString.Text = My.Settings.LastConnectionString
            CheckBox_Remember.Checked = True
        End If

        Application.DoEvents()
        If TextBox_ConnectionString.Text <> "" Then
            Button_Connect_Click(sender, e)
        End If
    End Sub

    Private Sub ListBox_Tables_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox_Tables.SelectedIndexChanged
        OK_Button.Enabled = True

    End Sub
End Class
